package linc.spring.demo.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * @author yqlin
 * @date 2021/9/7 01:38
 * @description BeanPostProcess表示Bena的后置处理器，我们可以定义一个或多个BeanPostProcessor
 * 一个BeanPostProcessor可以在任意一个Bean的初始化之前以及初始化之后去额外的做一些用户自定义的逻辑，
 * 当然，我们可以通过判断beanName来进行针对性处理（针对某个Bean，或某部分Bean
 */
@Component
public class CustomBeanPostProcessor implements BeanPostProcessor {
    /**
     * @PostConstruct之前
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if ("userService".equals(beanName)) {
            System.out.println("初始化前");
        }

        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }

    /**
     * InitializingBean之后
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if ("userService".equals(beanName)) {
            System.out.println("初始化后");
        }

        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }


}
